<template>
    <el-dialog @close="cancleEditSubject" v-model="dialogVisible" width="30%" title="修改设备点检保养项目" destroy-on-close center>
        <template #default>
            <el-form :model="subject" label-width="100px" label-position="left">
                <el-form-item label="项目编码">
                    <el-input v-model="subject.subjectCode" placeholder="请输入项目编码"/>
                </el-form-item>
                <el-form-item label="项目名称">
                    <el-input v-model="subject.subjectName" placeholder="请输入项目名称"/>
                </el-form-item>
                <el-form-item label="项目类型">
                    <el-radio-group v-model="subject.subjectType">
                        <el-radio value="设备点检">设备点检</el-radio>
                        <el-radio value="设备保养">设备保养</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="是否启用">
                    <el-radio-group v-model="subject.enableStatus">
                        <el-radio :value="1">是</el-radio>
                        <el-radio :value="2">否</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="项目内容">
                    <el-input type="textarea" v-model="subject.subjectContent" placeholder="请输入项目内容"/>
                </el-form-item>
                <el-form-item label="标准">
                    <el-input type="textarea" v-model="subject.subjectStandard" aria-placeholder="请输入标准" />
                </el-form-item>
            </el-form>
        </template>
        <!-- footer插槽中放两个按钮 -->
        <template #footer>
            <el-row>
                <el-col :span="12">
                    <el-button type="primary" @click="editSubject">确定</el-button>
                </el-col>
                <el-col :span="12">
                    <el-button type="primary" @click="cancleEditSubject">取消</el-button>
                </el-col>
            </el-row>
        </template>
    </el-dialog>
</template>
<script>
import { watch } from "vue";
import SubjectRequest from '../../../api/subject.js';
export default {
    props: ["visible","subjectId"],
    emits: ["optionCompleted"], // 对外暴露一个事件，用来传递添加学生的操作结果
    data() {
        return {
            subject: {},// 绑定要编辑的学生信息
            dialogVisible: false,// 编辑学生窗口的显示与隐藏
        }
    },
    methods: {
        /**
         * 取消添加学生的事件
         */
        cancleEditSubject(){
            // 通知父组件  操作完成，让父组件关闭窗体
            this.$emit("optionCompleted");
        },
        /**
         * 添加学生信息
         */
        editSubject(){
            SubjectRequest.updateSubject(this.subject,res=>{
                if (res.code == '200'){
                    this.$message({
                        type: 'success',
                        message: '编辑设备点检保养项目成功!'
                    });
                    // 通知父组件  操作完成，让父组件关闭窗体
                    this.$emit("optionCompleted");
                }else if(res.code == '300'){
                    this.$message({
                        type: 'warning',
                        message: '设备类型' + (res.msg.indexOf("subjectName") > 0 ?"名称":"类型") + "重复"
                    });
                }else{
                    this.$message({
                        type: 'error',
                        message: '编辑设备点检保养项目失败!'
                    });
                }
            });
        },

        /**
         * 加载学生信息
         */
        loadSubject(){
            SubjectRequest.getById(this.subjectId,res=>{
                console.log(res)
                if(res.data != null)
                    this.subject = res.data;
            });
        }
    }, mounted(props) {
        this.loadSubject()
        // 侦听props   一旦发生变化，就会出发下面的函数
        watch(() => this.visible, (newVal, oldVal) => {
            // 将最新的值设置给弹窗的dialogVisible属性
            this.dialogVisible = newVal;
        });

        watch(() => this.subjectId, (newVal, oldVal) => {
            // 如果toolId 发生变化，重新加载设备类型信息
            this.loadSubject();
        });
    }
}
</script>